package hgx.spring.cloud.lession.myhystrix;/**
 * Created by liujun on 2018/4/19.
 */

import hgx.spring.cloud.lession.myhystrix.exception.MyExceptionType;
import hgx.spring.cloud.lession.myhystrix.exception.MyHystrixHandlerException;
import hgx.spring.cloud.lession.productrepertory.vo.ProductVo;

/**
 * @author Administrator
 * @create 2018-04-19 14:44
 */
public class MyRequestHystrixCommand<R> extends BaseMyHystrixCommand {
    private MyRequest myRequest;


    public MyRequestHystrixCommand(MyRequest myRequest) {
        this.myRequest = myRequest;
    }

    @Override
    protected Object run(Object [] objs, MyCommandKeyType commandKeyType) {

        long startTime = System.currentTimeMillis();

        Object result = null;
        switch (commandKeyType) {
            case Create_Product:
                ProductVo productVo = (ProductVo)objs[0];
                result =myRequest.createProductVo(productVo);
                break;
            case Query_Products:
                result = myRequest.findAll();
                break;
            case Get_Product:
                Long id = (Long) objs[0];
                result = myRequest.getProductById(id);
                break;
        }
        int handlerTime = (int) (System.currentTimeMillis() - startTime);
        if (handlerTime > MyHystrixManager.Handler_Time_Out) {
            throw new MyHystrixHandlerException("处理请求超时", MyExceptionType.Time_Out);
        }

        return result;
    }


    @Override
    protected Object getFallback() {
        System.out.println("*********----  执行getFallback() 方法  -------**************");
        ProductVo productVo =new ProductVo();

        return productVo;
    }
}
